var KEYCODE_ENTER = 13;

var CHAT_REFRESH_INTERVAL = 5000;



/*---------------------------------------------------------------------------*\
 |                                    $()                                    |
\*---------------------------------------------------------------------------*/

$(function() {
    // Hooray, a page has been loaded!

    $('.education-link').click(scrollToEducation);
    $('.experience-link').click(scrollToExperience);
    $('#message-body').focus(focusMessage);
    $('#message-body').blur(blurMessage);
    $('#message-body').keyup(typing);
    if ($('#after').length > 0) {
        window.setInterval(refreshChat, CHAT_REFRESH_INTERVAL);
    }
    $('#clear-chat').submit(clearChat);
});



/*---------------------------------------------------------------------------*\
 |                            scrollToEducation()                            |
\*---------------------------------------------------------------------------*/

function scrollToEducation() {
    return scrollTo('#education');
}



/*---------------------------------------------------------------------------*\
 |                            scrollToExperience()                           |
\*---------------------------------------------------------------------------*/

function scrollToExperience() {
    return scrollTo('#experience');
}



/*---------------------------------------------------------------------------*\
 |                                 scrollTo()                                |
\*---------------------------------------------------------------------------*/

function scrollTo(id) {
    var elementToScroll = $.browser.safari ? 'body' : 'html';
    var offset = $(id).offset().top;
    $(elementToScroll).animate({scrollTop: offset}, 'slow', 'swing');
    return false;
}



/*---------------------------------------------------------------------------*\
 |                               focusMessage()                              |
\*---------------------------------------------------------------------------*/

function focusMessage() {
    var message = $('#message-body');
    var defaultMessage = message.prop('defaultValue');
    if (message.val() == defaultMessage) {
        message.val('');
    }
}



/*---------------------------------------------------------------------------*\
 |                               blurMessage()                               |
\*---------------------------------------------------------------------------*/

function blurMessage() {
    var message = $('#message-body');
    if (message.val() == '') {
        var defaultMessage = message.prop('defaultValue');
        message.val(defaultMessage);
    }
}



/*---------------------------------------------------------------------------*\
 |                                  typing()                                 |
\*---------------------------------------------------------------------------*/

function typing(eventObject) {
    var keycode = eventObject.which;
    if (keycode == KEYCODE_ENTER) {
        var body = $('#message-body').val();
        var after = $('#after').html();
        $('#message-body').val('');
        $.ajax({
            type: 'POST',
            url: '/chat',
            data: {
                body: body,
                after: after
            },
            cache: false,
            success: function(data, textStatus, xmlHttpRequest) {
                $('#after').remove();
                $('#messages').prepend(data);
                $('.message:hidden').slideDown();
            }
        });
        return false;
    }
}



/*---------------------------------------------------------------------------*\
 |                               refreshChat()                               |
\*---------------------------------------------------------------------------*/

function refreshChat() {
    var after = $('#after').html();
    $.ajax({
        type: 'GET',
        url: '/chat?after=' + after,
        cache: false,
        success: function(data, textStatus, xmlHttpRequest) {
            obj = $.parseJSON(data);

            $('#online').html(obj.status);

            $('#after').remove();
            $('#messages').prepend(obj.messages);
            $('.message:hidden').slideDown();
        }
    });
}



/*---------------------------------------------------------------------------*\
 |                                clearChat()                                |
\*---------------------------------------------------------------------------*/

function clearChat() {
    $.ajax({
        type: 'DELETE',
        url: '/chat',
        cache: false,
        success: function(data, textStatus, xmlHttpRequest) {
            window.location('/chat');
        }
    });
}
